﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml.Serialization;
using System.Data;
using System.Text;
using System.Reflection;
using System.Web.Services.Protocols;
using System.Xml;

/// <summary>
///BaseService 的摘要说明
/// </summary>
public class BaseService : System.Web.Services.WebService
{
    public List<T> DtToList<T>(DataTable dt)
    {
        List<T> li = new List<T>();
        T entity = default(T);
        String TempName = String.Empty;
        foreach (DataRow dr in dt.Rows)
        {
            entity = Activator.CreateInstance<T>();
            PropertyInfo[] PropertInfos = entity.GetType().GetProperties();
            foreach (PropertyInfo p in PropertInfos)
            {
                TempName = p.Name;
                if (!dt.Columns.Contains(TempName)) continue;
                if (!p.CanWrite) continue;

                object obj = dr[TempName];
                if (obj == null || obj == DBNull.Value) continue;

                if (obj == typeof(string))
                {
                    string Temp = obj.ToString();
                    p.SetValue(entity, Temp, null);
                }
                else if (obj == typeof(Int32))
                {
                    Int32 Temp = Convert.ToInt32(obj);
                    p.SetValue(entity, Temp, null);
                }

                else if (obj == typeof(long))
                {
                    long Temp = long.Parse(obj.ToString());
                    p.SetValue(entity, Temp, null);
                }
                else
                {
                    object Temp = Convert.ChangeType(obj, p.PropertyType);
                    p.SetValue(entity, Temp, null);
                }
            }
            li.Add(entity);
        }
        return li;
    }


    /// <summary>
    /// 异常类型
    /// </summary>
    public enum FaultCode
    {
        Client = 0,
        Server = 1
    }

    /// <summary>
    /// 封装异常为SoapException
    /// </summary>
    /// <param name="uri">引发异常的方法uri</param>
    /// <param name="errorMessage">错误信息</param>
    /// <param name="errorNumber">错误号</param>
    /// <param name="errorSource">错误源</param>
    /// <param name="code">异常类型</param>
    /// <returns>封装后的SoapException</returns>
    public SoapException RaiseException(string uri, string errorMessage, string errorNumber, string errorSource, FaultCode code, System.Exception exception,String  Parameter)
    {
        //初始化限定名
        XmlQualifiedName faultCodeLocation = null;

        //异常类型代码转换
        switch (code)
        {
            case FaultCode.Client:
                faultCodeLocation = SoapException.ClientFaultCode;
                break;
            case FaultCode.Server:
                faultCodeLocation = SoapException.ServerFaultCode;
                break;
        }

        //构建异常信息结构体
        string strXmlOut = @"<detail>"
                         + "<Error>"
                         + "<ErrorNumber>" + errorNumber + "</ErrorNumber>"
                         + "<ErrorMessage>" + errorMessage + "</ErrorMessage>"
                         + "<ErrorSource>" + errorSource + "</ErrorSource>"
                         + "</Error>"
                         + "</detail>";

        //装载为Xml文档
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(strXmlOut);

        //实例化SoapException
        SoapException soapEx = new SoapException(errorMessage, faultCodeLocation, uri, xmlDoc.DocumentElement);

        RoadTeam.Model.SmException EX = new RoadTeam.Model.SmException();
        EX.ExceptionMsg = exception.Message;
        EX.ExceptionStackTrace = exception.StackTrace.Replace(" 在", "<br/>在");

        EX.TargetSiteName = exception.TargetSite.Name;
        EX.TargetSiteFullName = exception.TargetSite.DeclaringType.FullName;
        EX.ExceptionSource = exception.Source;

        EX.MethodName = uri;
        EX.Parameter = Parameter;

        RoadTeam.BLL.SmExceptionBll bll = new RoadTeam.BLL.SmExceptionBll();
        bll.InSertException(EX);

        //返回SoapException
        return soapEx;
    }

    #region 原来的代码 已经注释

    #region 原来验房单项目 停用的代码
    //[WebMethod(Description = "插入[验房单集]返回验房项目数据集对象,带唯一标识符号123123")]
    // [SoapDocumentMethod]
    //public CsInspectHouse InsertCsAcceptItem(String Json)
    //{
    //    InspectHouseBLL bll = new InspectHouseBLL();
    //    JavaScriptSerializer jscvt = new JavaScriptSerializer();
    //    CsInspectHouse model = jscvt.Deserialize<CsInspectHouse>(Json);
    //    if (model.Item.Length == 0) return new CsInspectHouse();
    //    DataTable dt = bll.GetCsInspectHouseDataForEnptyTable();
    //    DataRow dr;
    //    foreach (InspectHouse item in model.Item)
    //    {
    //        dr = dt.NewRow();
    //        dr["InspectHouseIdentifier"] = CreateIdentifier.GetIdentifier();
    //        dr["InspectHouseNo"] = item.InspectHouseNo;
    //        dr["InspectHouseDate"] = item.InspectHouseDate;
    //        dr["InspectHouseUnitNo"] = item.InspectHouseUnitNo;
    //        dr["InspectHouseSituation"] = item.InspectHouseSituation;
    //        dr["InspectHouseAddress"] = item.InspectHouseAddress;
    //        dr["InspectHouseContact"] = item.InspectHouseContact;
    //        dr["InspectHouseContactPhone"] = item.InspectHouseContactPhone;
    //        dr["InspectHouseCreater"] = item.InspectHouseCreater;
    //        dr["InspectHouseCreatDate"] = item.InspectHouseCreatDate;
    //        dr["InspectHouseRemark"] = item.InspectHouseRemark;
    //        item.InspectHouseIdentifier = dr["InspectHouseIdentifier"].ToString();
    //        dt.Rows.Add(dr);
    //    }
    //    if (bll.Insert(dt, "方法名[InsertCsInspectHouse]" + Json))
    //        return model;
    //    else
    //        return new CsInspectHouse();
    //}
    //[WebMethod(Description = "插入[验房单集]返回验房项目数据集对象,带唯一标识符号")]
    //[SoapDocumentMethod]
    //public CsInspectHouse InsertCsInspectHouse(String Json)
    //{
    //    InspectHouseBLL bll = new InspectHouseBLL();
    //    JavaScriptSerializer jscvt = new JavaScriptSerializer();
    //    CsInspectHouse model = jscvt.Deserialize<CsInspectHouse>(Json);
    //    if (model.Item.Length == 0) return new CsInspectHouse();
    //    DataTable dt = bll.GetCsInspectHouseDataForEnptyTable();
    //    DataRow dr;
    //    foreach (InspectHouse item in model.Item)
    //    {
    //        dr = dt.NewRow();
    //        dr["InspectHouseIdentifier"] = CreateIdentifier.GetIdentifier();
    //        dr["InspectHouseNo"] = item.InspectHouseNo;
    //        dr["InspectHouseDate"] = item.InspectHouseDate;
    //        dr["InspectHouseUnitNo"] = item.InspectHouseUnitNo;
    //        dr["InspectHouseSituation"] = item.InspectHouseSituation;
    //        dr["InspectHouseAddress"] = item.InspectHouseAddress;
    //        dr["InspectHouseContact"] = item.InspectHouseContact;
    //        dr["InspectHouseContactPhone"] = item.InspectHouseContactPhone;
    //        dr["InspectHouseCreater"] = item.InspectHouseCreater;
    //        dr["InspectHouseCreatDate"] = item.InspectHouseCreatDate;
    //        dr["InspectHouseRemark"] = item.InspectHouseRemark;
    //        item.InspectHouseIdentifier = dr["InspectHouseIdentifier"].ToString();
    //        dt.Rows.Add(dr);
    //    }
    //    if (bll.Insert(dt, "方法名[InsertCsInspectHouse]" + Json))
    //        return model;
    //    else
    //        return new CsInspectHouse();
    //}
    //[WebMethod(Description = "获取验房项目数据集合")]
    //[SoapDocumentMethod]
    //public CsListItem GetCsListItemData()
    //{
    //    CsListItemBLL bll = new CsListItemBLL();
    //    DataTable dt = bll.GetCsListItemData();
    //    CsListItemTemp temp;
    //    List<CsListItemTemp> li = new List<CsListItemTemp>();
    //    foreach (DataRow dr in dt.Rows)
    //    {
    //        temp = new CsListItemTemp();
    //        temp.ListItemIdentifier = dr["ListItemIdentifier"].ToString();
    //        temp.ListItemName = dr["ListItemName"].ToString();
    //        temp.ListItemContent = dr["ListItemContent"].ToString();
    //        temp.ListItemRemark = dr["ListItemRemark"].ToString();
    //        li.Add(temp);
    //    }
    //    CsListItem listitem = new CsListItem();
    //    listitem.Item = li.ToArray();
    //    return listitem;
    //}
    #endregion
    #endregion



}

